<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="150px">
      <el-form-item label="司机名称">
        <el-input v-model="formInline.driverName" placeholder="司机名称" ></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.driverPhone" placeholder="手机号" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
        <el-button type="primary" @click="openadd('formInline')">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="driverId"
        label="序号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="driverName"
        label="司机名称"
        width="120">
      </el-table-column>
      <el-table-column
        prop="driverPhone"
        label="手机号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="hitchDriver"
        label="已挂接车辆"
        width="120">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄"
        width="100">
      </el-table-column>
      <el-table-column
        prop="drivingLicense"
        label="驾照"
        width="100">
      </el-table-column>
      <el-table-column
        prop="mileage"
        label="工作总里程"
        width="120">
      </el-table-column>
      <el-table-column
        prop="duration"
        label="工作总时长"
        width="120">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sex"
        label="司机性别"
        width="100">
        <template v-slot="s">
          <el-tag v-if="s.row.sex==0">男</el-tag>
          <el-tag v-if="s.row.sex==1">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="driverCard"
        label="身份证号"
        width="180">
      </el-table-column>
      <el-table-column
        label="登记照"
        width="120">
        <template v-slot="s">
          <el-image
            style="width: 100px; height: 100px"
            :src="s.row.driverAvatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="350">
        <template slot-scope="scope">
          <el-button @click="driverUpdate(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="viewSchedule(scope.row)" type="success" size="small">查看排班</el-button>
          <el-button @click="viewRoute(scope.row)" type="info" size="small">线路出行情况</el-button>
          <el-button @click="driverDelete(scope.row.driverId)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="添加司机"  :visible.sync="dialogFormVisible">
      <el-form :model="addForm" :rules="rules" ref="formName">
        <el-form-item label="司机名称" prop="driverName">
          <el-input v-model="addForm.driverName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="driverPhone">
          <el-input v-model="addForm.driverPhone"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="driverCard">
          <el-input v-model="addForm.driverCard"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="addForm.age"></el-input>
        </el-form-item>
        <el-form-item label="客户性别">
          <el-radio v-model="addForm.sex" label=0>男</el-radio>
          <el-radio v-model="addForm.sex" label=1>女</el-radio>
        </el-form-item>
        <el-form-item label="驾照">
          <el-input v-model="addForm.drivingLicense"></el-input>
        </el-form-item>
        <el-form-item label="登记照">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:10020/driver/driverUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :befor-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('formName')">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改司机"  :visible.sync="updateFormVisible">
      <el-form :model="updateForm">
        <el-form-item label="司机名称">
          <el-input v-model="updateForm.driverName"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="updateForm.driverPhone"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="updateForm.driverCard"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="updateForm.age"></el-input>
        </el-form-item>
        <el-form-item label="客户性别">
          <el-radio v-model="updateForm.sex" label=0>男</el-radio>
          <el-radio v-model="updateForm.sex" label=1>女</el-radio>
        </el-form-item>
        <el-form-item label="驾照">
          <el-input v-model="updateForm.drivingLicense"></el-input>
        </el-form-item>
        <el-form-item label="登记照">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:10020/driver/driverUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :befor-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'DriverView',
  data(){
    return{
      formInline:{},
      tableData:[],
      pageNum:1,
      pageSize:3,
      total:0,
      dialogFormVisible:false,
      updateFormVisible:false,
      updateForm:{},
      addForm:{},
      imageUrl:'',
      rules:{
        driverName: [
          { required: true, message: '请输入司机名称', trigger: 'blur' },
        ],
        driverPhone: [
          { required: true, message: '请选择手机号', trigger: 'blur' }
        ],
        driverCard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ],
      }
    }
  },
  methods:{
    list(){
      this.axios.post("/driver/driverList?pageNum="+this.pageNum+"&pageSize="+this.pageSize,this.formInline).then(r=>{
          this.tableData=r.data.data.list;
          this.total=r.data.data.total;
      })
    },
    openadd(){
      this.imageUrl="";
      this.dialogFormVisible=true;
      this.addForm={sex:'0'};
    },
    add(formName){
      this.$refs[formName].validate((valid)=>{
        if (valid){
          this.axios.post("/driver/driverAdd",this.addForm).then(r=>{
            if (r.data.code==200){
              this.dialogFormVisible=false;
              this.$message.success(r.data.msg);
              this.list();
            }else {
              this.$message.error(r.data.msg);
            }
          })
        }else {
          console.log('error submit!!!');
          return false;
        }
      })
    },
    driverUpdate(row){
      this.updateFormVisible=true;
      this.imageUrl=row.driverAvatar;
      this.updateForm=row;
    },
    update(){
      this.axios.post("/driver/driverUpdate",this.updateForm).then(r=>{
        if (r.data.code==200){
          this.updateFormVisible=false;
          this.$message.success(r.data.msg);
          this.list();
        }else {
          this.$message.error(r.data.msg);
        }
      })
    },
    handleSizeChange(val){
      this.pageSize=val
      this.list();
    },
    handleCurrentChange(val){
      this.pageNum=val
      this.list();
    },
    handleAvatarSuccess(res,file){
      this.imageUrl=URL.createObjectURL(file.raw)
      console.log(res)
      this.addForm.driverAvatar=res

    },
    beforeAvatarUpload(file){
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    driverDelete(driverId){
      this.axios.post("/driver/driverDelete?driverId="+driverId).then(r=>{
        if (r.data.code==200){
          this.$message.success(r.data.msg);
          this.list();
        }else {
          this.$message.error(r.data.msg);
          this.list();
        }
      })
    },
    viewSchedule(){

    },
    viewRoute(){

    }
  },
  created() {
    this.list();
  }
}
</script>

<style scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
